package yartiss.engine.simulation.system.taskset.generation.task;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import yartiss.engine.simulation.system.ITask;
import yartiss.engine.simulation.system.schedulable.SchedulableFactory;
import yartiss.engine.simulation.system.taskset.generation.ITaskGenerator;

public class FileTaskGenerator implements ITaskGenerator {

	private List<ITask> tasks;
	private int currentTask = 0;
	
	public FileTaskGenerator(String file){
		tasks  = readTasks(file);
	}
	
	
	@Override
	public ITask generate(double utilization, double energyUtilization, long pr,	boolean offset, int deadlineModel, int maxProc) {
		ITask task = tasks.get(currentTask++); 
		currentTask %= tasks.size();
		return task;
	}
	
	private List<ITask> readTasks(String file){
		List<ITask> filesTasks = new ArrayList<ITask>();
		Scanner scanner;
		try {
			scanner = new Scanner(new File(file));
			while(scanner.hasNextLine()){
				String line = scanner.nextLine();
				String [] params = line.split(" ");
				long start 		= Long.parseLong(params[0]);
				long wcet 		= Long.parseLong(params[1]);
				long wcee 		= Long.parseLong(params[2]);
				long period		= Long.parseLong(params[3]);
				long deadline	= Long.parseLong(params[4]);
				int priority 	= Integer.parseInt(params[5]);
				
				filesTasks.add( SchedulableFactory.newInstance("simple", start, wcet, wcee, period, deadline, priority));
			}
			
			scanner.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return filesTasks;
	}

}
